
#include <rtthread.h>
#include "board.h"
#include "board_virtualIO.h"



static uint8_t out1;
static uint8_t out2;
static uint8_t out3;

static uint8_t out_buff[2] = {0};

void power_init(void)
{
    out1 = rt_pin_get("PA.6");
    out2 = rt_pin_get("PE.0");
    out3 = rt_pin_get("PE.1");

    rt_pin_mode(out1, PIN_MODE_OUTPUT);
    rt_pin_mode(out2, PIN_MODE_OUTPUT);
    rt_pin_mode(out3, PIN_MODE_OUTPUT);
}

void power_update(void)
{
    vio_read_hold_coils(0,3,out_buff);  //

    rt_pin_write(out1, dio_get_val(out_buff, 0) );
    rt_pin_write(out2, dio_get_val(out_buff, 1) );
    rt_pin_write(out3, dio_get_val(out_buff, 2) );
}


void oled_update(void);

static uint8_t key1;
static uint8_t key2;
static uint8_t key3;

static uint8_t key_buff[2] = {0};
static uint8_t key_buff_last[2] = {0};

void key_init(void)
{
    key1 = rt_pin_get("PF.1");
    key2 = rt_pin_get("PF.0");
    key3 = rt_pin_get("PA.2");

    rt_pin_mode(key1, PIN_MODE_INPUT);
    rt_pin_mode(key2, PIN_MODE_INPUT);
    rt_pin_mode(key3, PIN_MODE_INPUT);
}

void key_scan(void)
{
    int change = 0;
    int state = 0;
    uint8_t swap_iobuff[2] = {0};

    dio_set_val(key_buff, 0, !rt_pin_read(key1));
    dio_set_val(key_buff, 1, !rt_pin_read(key2));
    dio_set_val(key_buff, 2, !rt_pin_read(key3));

    vio_lowlevel_update_input_coils(0,3,key_buff);  //更新vio的输入线圈部分数据


    state = dio_get_val(key_buff,0);
    if(state != dio_get_val(key_buff_last,0))
    {
        dio_set_val(key_buff_last,0,state);
        if(state == 1)
        {
            vio_read_hold_coils(0,1,swap_iobuff);
            dio_set_val(swap_iobuff,0, ! dio_get_val(swap_iobuff,0));
            vio_write_hold_coils(0,1,swap_iobuff);
        }
        change++;
    }

    state = dio_get_val(key_buff,1);
    if(state != dio_get_val(key_buff_last,1))
    {
        dio_set_val(key_buff_last,1,state);
        if(state == 1)
        {
            vio_read_hold_coils(1,1,swap_iobuff);
            dio_set_val(swap_iobuff,0, ! dio_get_val(swap_iobuff,0));
            vio_write_hold_coils(1,1,swap_iobuff);
        }
        change++;
    }

    state = dio_get_val(key_buff,2);
    if(state != dio_get_val(key_buff_last,2))
    {
        dio_set_val(key_buff_last,2,state);
        if(state == 1)
        {
            vio_read_hold_coils(2,1,swap_iobuff);
            dio_set_val(swap_iobuff,0, ! dio_get_val(swap_iobuff,0));
            vio_write_hold_coils(2,1,swap_iobuff);
        }
        change++;
    }

    if(change > 0)
    {
        oled_update();
    }
}

#define ADC_DEV_NAME        "adc0"      /* ADC 设备名称 */
#define ADC_DEV_CHANNEL     0           /* ADC 通道 */

#define REFER_VOLTAGE       3300.0         /* 参考电压 3.3V,数据精度乘以1000保留3位小数*/
#define CONVERT_BITS        (1 << 10)   /* 转换位数为12位 */

float       adc_acc = REFER_VOLTAGE / CONVERT_BITS;
float       adc_vol = 0;

static rt_adc_device_t adc_dev;
static uint32_t  adc_channel[5] = {4, 3, 9,8,7};

static uint16_t  adc_channel_val[5] = {0};

static uint16_t  adc_cal_val[5] = {0};

void adc_init(void)
{
    int ret = 0;
    /* 查找设备 */
    adc_dev = (rt_adc_device_t)rt_device_find(ADC_DEV_NAME);
    if (adc_dev == RT_NULL)
    {
        rt_kprintf("adc sample run failed! can't find %s device!\n", ADC_DEV_NAME);
        return;
    }
    /* 使能设备 */
    ret = rt_adc_enable(adc_dev, adc_channel[0]);
    ret = rt_adc_enable(adc_dev, adc_channel[1]);
    ret = rt_adc_enable(adc_dev, adc_channel[2]);
    ret = rt_adc_enable(adc_dev, adc_channel[3]);
    ret = rt_adc_enable(adc_dev, adc_channel[4]);
}

void adc_scan(void)
{
    adc_channel_val[0] = rt_adc_read(adc_dev, adc_channel[0]);
    adc_channel_val[1] = rt_adc_read(adc_dev, adc_channel[1]);
    adc_channel_val[2] = rt_adc_read(adc_dev, adc_channel[2]);
    adc_channel_val[3] = rt_adc_read(adc_dev, adc_channel[3]);
    adc_channel_val[4] = rt_adc_read(adc_dev, adc_channel[4]);

    vio_lowlevel_update_input_regs(5,5,adc_channel_val);  //更新vio的输入寄存器部分数据

    if(adc_channel_val[0] > 60)
    {
        adc_acc = 2500.0 / adc_channel_val[0] ;  //tl431 2.5v
    }else {
        adc_acc = REFER_VOLTAGE / CONVERT_BITS ;  //3.3v
    }
    adc_vol = adc_channel_val[1] * adc_acc * 2;  //5V USB IN
    adc_cal_val[0] = adc_vol;

    adc_vol = adc_channel_val[2] * adc_acc;  // USB1
    adc_cal_val[1] = adc_vol;

    adc_vol = adc_channel_val[3] * adc_acc;  // USB2
    adc_cal_val[2] = adc_vol;

    adc_vol = adc_channel_val[4] * adc_acc;  // USB3
    adc_cal_val[3] = adc_vol;

    vio_lowlevel_update_input_regs(0,5,adc_cal_val);  //更新vio的输入寄存器部分数据
}


static void scanio_entry(void *param)
{
    rt_kprintf("scanio init\n");
    power_init();
    key_init();
    adc_init();
    while (1)
    {
        rt_thread_mdelay(50);
        power_update();
        key_scan();
        adc_scan();
    }
}


void board_scanio_init(void)
{
    rt_thread_t tid1 = rt_thread_create("scanio",scanio_entry, RT_NULL,2048,9, 10);
    if (tid1 != RT_NULL)
    {
        rt_thread_startup(tid1);
    }

}


